package ilvonen.ihstatistics.repository;

import ilvonen.ihstatistics.models.Game;
import ilvonen.ihstatistics.models.Team;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

public class DBGames {
	//define table name
		private static final String TABLE_NAME = "games";
	//define tables column names
		private static final String KEY_ID = "_id";
		private static final String KEY_TEAM_ID = "team_id";
		private static final String KEY_LEAGUE_ID = "league_id";
		private static final String KEY_SEASON_ID = "season_id";
		private static final String KEY_SEASON_TYPE_ID = "season_type_id";
		private static final String KEY_OPPONENT = "opponent";
		private static final String KEY_DATE = "date";
		private static final String KEY_HAS_GAME_ENDED = "has_game_ended";
		private static final String KEY_GOALS = "goals";
		private static final String KEY_GOALS_AGAINST = "goals_against";
		private static final String KEY_SHOTS = "shots";
		private static final String KEY_SHOTS_AGAINST = "shots_against";
		private static final String KEY_PENALTY_MINUTES = "penalty_minutes";
		private static final String KEY_POWERPLAY_MINUTES = "powerplay_minutes";
		private static final String KEY_FACEOFF_WINS = "faceoff_wins";
		private static final String KEY_FACEOFF_LOSSES = "faceoff_losses";
	//define create statement for all your tables. Create new file (class) for each table.
		private static final String CREATE_STATEMENT = "create table if not exists " + TABLE_NAME + " ("
														+ KEY_ID + " integer primary key autoincrement, "
														+ KEY_TEAM_ID + " integer not null, "
														+ KEY_LEAGUE_ID + " integer not null, "
														+ KEY_SEASON_ID + " integer not null, "
														+ KEY_SEASON_TYPE_ID + " integer not null, "
														+ KEY_OPPONENT + " text not null, "
														+ KEY_DATE + " text not null, "
														+ KEY_HAS_GAME_ENDED + " integer not null, "
														+ KEY_GOALS + " integer, "
														+ KEY_GOALS_AGAINST + " integer, "
														+ KEY_SHOTS + " integer, "
														+ KEY_SHOTS_AGAINST + " integer, "
														+ KEY_PENALTY_MINUTES + " integer, "
														+ KEY_POWERPLAY_MINUTES + " integer, "
														+ KEY_FACEOFF_WINS + " integer, "
														+ KEY_FACEOFF_LOSSES + " integer);";
	
	/*
	 * CLASS STATUS: READY?NOT
	 */
	public DBGames(){}
	/*
	 * 
	 */
	public String create(){
		return CREATE_STATEMENT;
	}
	/*
	 * 
	 */
	public long insert(String opponent, String date, int team, int league, int season, int seasontype, int gameover, DBAdapter db){
		/*
		 * Only these few columns are used when inserting new game to database. These are all not null fields. Null fields must be updated after game has ended.
		 */
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_TEAM_ID, team);
		initialValues.put(KEY_LEAGUE_ID, league);
		initialValues.put(KEY_SEASON_ID, season);
		initialValues.put(KEY_SEASON_TYPE_ID, seasontype);
		initialValues.put(KEY_OPPONENT, opponent);
		initialValues.put(KEY_DATE, date);
		initialValues.put(KEY_HAS_GAME_ENDED, gameover);
		
		return db.insertRecord(TABLE_NAME, initialValues);
		
	}
	/*
	 * 
	 */
	public boolean delete(long rowId, DBAdapter db){
		return db.deleteRecord(TABLE_NAME, KEY_ID, rowId);
	}
	
	/*
	 * 
	 */
	public void update(long gameId, int gameOver, int goals, int goalsagainst, int shots, int shotsagainst, int pptime, int shtime, int faceoffw, int faceoffl, DBAdapter db) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_HAS_GAME_ENDED, gameOver);
		initialValues.put(KEY_GOALS, goals);
		initialValues.put(KEY_GOALS_AGAINST, goalsagainst);
		initialValues.put(KEY_SHOTS, shots);
		initialValues.put(KEY_SHOTS_AGAINST, shotsagainst);
		initialValues.put(KEY_POWERPLAY_MINUTES, pptime);
		initialValues.put(KEY_PENALTY_MINUTES, shtime);
		initialValues.put(KEY_FACEOFF_WINS, faceoffw);
		initialValues.put(KEY_FACEOFF_LOSSES, faceoffl);
		db.updateRecord(TABLE_NAME, initialValues, KEY_ID, gameId);
	}
	/*
	 * 
	 */
	public List<Game> getAllGames(DBAdapter db){
		Cursor c;
		List<Game> games = new ArrayList<Game>();
		c = db.getAllRecords(TABLE_NAME, new String[]{KEY_ID,KEY_TEAM_ID,KEY_LEAGUE_ID,KEY_SEASON_ID,KEY_SEASON_TYPE_ID,KEY_OPPONENT,KEY_DATE,KEY_HAS_GAME_ENDED,KEY_GOALS,KEY_GOALS_AGAINST,KEY_SHOTS,KEY_SHOTS_AGAINST,KEY_PENALTY_MINUTES,KEY_POWERPLAY_MINUTES,KEY_FACEOFF_WINS,KEY_FACEOFF_LOSSES});
		while(c.moveToNext()){
			Game g = new Game();
			g.set_id(c.getInt(0));
			g.set_teamid(c.getInt(1));
			g.set_leagueid(c.getInt(2));
			g.set_seasonid(c.getInt(3));
			g.set_seasontypeid(c.getInt(4));
			g.set_opponent(c.getString(5));
			g.set_date(c.getString(6));
			g.set_hasGameEnded(c.getInt(7));
			g.set_goals(c.getInt(8));
			g.set_goalsAgainst(c.getInt(9));
			g.set_shots(c.getInt(10));
			g.set_shotsAgainst(c.getInt(11));
			g.set_penaltyMinutes(c.getInt(12));
			g.set_powerplayMinutes(c.getInt(13));
			g.set_faceoffWins(c.getInt(14));
			g.set_faceoffLosses(c.getInt(15));
			games.add(g);
		}
		return games;
	}
	/*
	 * 
	 */
	public List<Game> getAllGamesBySeasonId(DBAdapter db, long ROW_ID){
		List<Game> games = new ArrayList<Game>();
		Cursor c;
		c = db.getAllRecordsById(TABLE_NAME, new String[]{KEY_ID,KEY_TEAM_ID,KEY_LEAGUE_ID,KEY_SEASON_ID,KEY_SEASON_TYPE_ID,KEY_OPPONENT,KEY_DATE,KEY_HAS_GAME_ENDED,KEY_GOALS,KEY_GOALS_AGAINST,KEY_SHOTS,KEY_SHOTS_AGAINST,KEY_PENALTY_MINUTES,KEY_POWERPLAY_MINUTES,KEY_FACEOFF_WINS,KEY_FACEOFF_LOSSES}, KEY_SEASON_ID, ROW_ID);
		while(c.moveToNext()){
			Game g = new Game();
			g.set_id(c.getInt(0));
			g.set_teamid(c.getInt(1));
			g.set_leagueid(c.getInt(2));
			g.set_seasonid(c.getInt(3));
			g.set_seasontypeid(c.getInt(4));
			g.set_opponent(c.getString(5));
			g.set_date(c.getString(6));
			g.set_hasGameEnded(c.getInt(7));
			g.set_goals(c.getInt(8));
			g.set_goalsAgainst(c.getInt(9));
			g.set_shots(c.getInt(10));
			g.set_shotsAgainst(c.getInt(11));
			g.set_penaltyMinutes(c.getInt(12));
			g.set_powerplayMinutes(c.getInt(13));
			g.set_faceoffWins(c.getInt(14));
			g.set_faceoffLosses(c.getInt(15));
			games.add(g);
		}
		return games;
	}
	/*
	 * 
	 */
	public List<Game> getAllGamesByLeagueId(DBAdapter db, long ROW_ID){
		List<Game> games = new ArrayList<Game>();
		Cursor c;
		c = db.getAllRecordsById(TABLE_NAME, new String[]{KEY_ID,KEY_TEAM_ID,KEY_LEAGUE_ID,KEY_SEASON_ID,KEY_SEASON_TYPE_ID,KEY_OPPONENT,KEY_DATE,KEY_HAS_GAME_ENDED,KEY_GOALS,KEY_GOALS_AGAINST,KEY_SHOTS,KEY_SHOTS_AGAINST,KEY_PENALTY_MINUTES,KEY_POWERPLAY_MINUTES,KEY_FACEOFF_WINS,KEY_FACEOFF_LOSSES}, KEY_LEAGUE_ID, ROW_ID);
		while(c.moveToNext()){
			Game g = new Game();
			g.set_id(c.getInt(0));
			g.set_teamid(c.getInt(1));
			g.set_leagueid(c.getInt(2));
			g.set_seasonid(c.getInt(3));
			g.set_seasontypeid(c.getInt(4));
			g.set_opponent(c.getString(5));
			g.set_date(c.getString(6));
			g.set_hasGameEnded(c.getInt(7));
			g.set_goals(c.getInt(8));
			g.set_goalsAgainst(c.getInt(9));
			g.set_shots(c.getInt(10));
			g.set_shotsAgainst(c.getInt(11));
			g.set_penaltyMinutes(c.getInt(12));
			g.set_powerplayMinutes(c.getInt(13));
			g.set_faceoffWins(c.getInt(14));
			g.set_faceoffLosses(c.getInt(15));
			games.add(g);
		}
		return games;
	}
	/*
	 * 
	 */
	public List<Game> getAllGamesByTeamId(DBAdapter db, long ROW_ID){
		List<Game> games = new ArrayList<Game>();
		Cursor c;
		c = db.getAllRecordsById(TABLE_NAME, new String[]{KEY_ID,KEY_TEAM_ID,KEY_LEAGUE_ID,KEY_SEASON_ID,KEY_SEASON_TYPE_ID,KEY_OPPONENT,KEY_DATE,KEY_HAS_GAME_ENDED,KEY_GOALS,KEY_GOALS_AGAINST,KEY_SHOTS,KEY_SHOTS_AGAINST,KEY_PENALTY_MINUTES,KEY_POWERPLAY_MINUTES,KEY_FACEOFF_WINS,KEY_FACEOFF_LOSSES}, KEY_TEAM_ID, ROW_ID);
		while(c.moveToNext()){
			Game g = new Game();
			g.set_id(c.getInt(0));
			g.set_teamid(c.getInt(1));
			g.set_leagueid(c.getInt(2));
			g.set_seasonid(c.getInt(3));
			g.set_seasontypeid(c.getInt(4));
			g.set_opponent(c.getString(5));
			g.set_date(c.getString(6));
			g.set_hasGameEnded(c.getInt(7));
			g.set_goals(c.getInt(8));
			g.set_goalsAgainst(c.getInt(9));
			g.set_shots(c.getInt(10));
			g.set_shotsAgainst(c.getInt(11));
			g.set_penaltyMinutes(c.getInt(12));
			g.set_powerplayMinutes(c.getInt(13));
			g.set_faceoffWins(c.getInt(14));
			g.set_faceoffLosses(c.getInt(15));
			games.add(g);
		}
		return games;
	}
	public Game getGameByGameId(DBAdapter db, long ROW_ID){
		Game g = null;
		Cursor c;
		c = db.getRecord(TABLE_NAME, new String[]{KEY_ID,KEY_TEAM_ID,KEY_LEAGUE_ID,KEY_SEASON_ID,KEY_SEASON_TYPE_ID,KEY_OPPONENT,KEY_DATE,KEY_HAS_GAME_ENDED,KEY_GOALS,KEY_GOALS_AGAINST,KEY_SHOTS,KEY_SHOTS_AGAINST,KEY_PENALTY_MINUTES,KEY_POWERPLAY_MINUTES,KEY_FACEOFF_WINS,KEY_FACEOFF_LOSSES}, KEY_ID, ROW_ID);
		if(c != null){
			g = new Game();
			c.moveToFirst();
			g.set_id(c.getInt(0));
			g.set_teamid(c.getInt(1));
			g.set_leagueid(c.getInt(2));
			g.set_seasonid(c.getInt(3));
			g.set_seasontypeid(c.getInt(4));
			g.set_opponent(c.getString(5));
			g.set_date(c.getString(6));
			g.set_hasGameEnded(c.getInt(7));
			g.set_goals(c.getInt(8));
			g.set_goalsAgainst(c.getInt(9));
			g.set_shots(c.getInt(10));
			g.set_shotsAgainst(c.getInt(11));
			g.set_penaltyMinutes(c.getInt(12));
			g.set_powerplayMinutes(c.getInt(13));
			g.set_faceoffWins(c.getInt(14));
			g.set_faceoffLosses(c.getInt(15));
		}
		return g;
	}

}
